package com.engine.game;

import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.component.game.Component;

public class InputHandler implements InputProcessor {
	
	private GameEngine engine;

	public GameEngine getEngine() {
		return engine;
	}

	public void setEngine(GameEngine engine) {
		this.engine = engine;
	}

	@Override
	public boolean keyDown(int keycode) {
		engine.pushEvent(Event.createEvent("INPUT_HANDLER", "key;" + Keys.toString(keycode) + ";went_down" , Component.Input));
		
		
		return true;
	}

	@Override
	public boolean keyUp(int keycode) {
		engine.pushEvent(Event.createEvent("INPUT_HANDLER", "key;" + Keys.toString(keycode) + ";went_up" , Component.Input));
		
		return true;
	}

	@Override
	public boolean keyTyped(char character) {
		
		return true;
	}

	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
		
		switch(button){
		case Buttons.LEFT :
			engine.pushEvent(Event.createEvent("INPUT_HANDLER", "touch_down_on;" + screenX + ";" +(Gdx.graphics.getHeight()-screenY) + ";left" , Component.Input));
			break;
		case Buttons.MIDDLE :
			engine.pushEvent(Event.createEvent("INPUT_HANDLER", "touch_down_on;" + screenX + ";" +(Gdx.graphics.getHeight()-screenY) + ";middle" , Component.Input));
			break;
		case Buttons.RIGHT :
			engine.pushEvent(Event.createEvent("INPUT_HANDLER", "touch_down_on;" + screenX + ";" +(Gdx.graphics.getHeight()-screenY) + ";right" , Component.Input));
			break;
		default:
			break;	
			
		}

		
		return true;
	}

	@Override
	public boolean touchUp(int screenX, int screenY, int pointer, int button) {
		switch(button){
		case Buttons.LEFT :
			engine.pushEvent(Event.createEvent("INPUT_HANDLER", "touch_up_on;" + screenX + ";" +(Gdx.graphics.getHeight()-screenY) + ";left" , Component.Input));
			break;
		case Buttons.MIDDLE :
			engine.pushEvent(Event.createEvent("INPUT_HANDLER", "touch_up_on;" + screenX + ";" +(Gdx.graphics.getHeight()-screenY) + ";middle" , Component.Input));
			break;
		case Buttons.RIGHT :
			engine.pushEvent(Event.createEvent("INPUT_HANDLER", "touch_up_on;" + screenX + ";" +(Gdx.graphics.getHeight()-screenY) + ";right" , Component.Input));
			break;
		default:
			break;	
			
		}
		
		return true;
	}

	@Override
	public boolean touchDragged(int screenX, int screenY, int pointer) {
		
		return true;
	}

	@Override
	public boolean mouseMoved(int screenX, int screenY) {
	engine.pushEvent(Event.createEvent("INPUT_HANDLER", "mouse_moved_on;" + screenX + ";" +(Gdx.graphics.getHeight()-screenY), Component.Input));
		
		return true;
	}

	@Override
	public boolean scrolled(int amount) {
		switch(amount){
		case -1 :
			engine.pushEvent(Event.createEvent("INPUT_HANDLER", "scrolled_up", Component.Input));
			break;
		case 1 :
			engine.pushEvent(Event.createEvent("INPUT_HANDLER", "scrolled_down", Component.Input));
			break;
		}
		
		return true;
	}

}
